public class BinarySearchTree {
    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }
    public TreeNode root;

    public void insert(int key) {
        if(root==null){
            root=new TreeNode(key);
            return;
        }
        TreeNode cur=root;
        TreeNode parent=null;
        while(cur!=null){
            if(cur.key<key){
                parent=cur;
                cur=cur.right;
            } else if (cur.key==key) {
                return;
            }else{
                parent=cur;
                cur=cur.left;
            }
        }
        TreeNode node=new TreeNode(key);
        if(parent.key<key){
            parent.right=node;
        }else{
            parent.left=node;
        }
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur=root;
        while(cur!=null){
            if(cur.key<key){
                cur=cur.right;
            } else if (cur.key==key) {
                return cur;
            }else{
                cur=cur.left;
            }
        }
        return null;
    }
    //删除key的值
    public void remove(int key) {
        TreeNode cur=root;
        TreeNode parent=null;
        while(cur!=null){
            if(cur.key==key){
                removeNode(parent,cur);
                return;
            } else if (cur.key<key) {
                parent=cur;
                cur=cur.right;
            }else{
                parent=cur;
                cur=cur.left;
            }
        }
    }
    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(parent.left == cur) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(parent.left == cur) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.key = target.key;
            if(target == targetParent.left) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }

    public void inorder(TreeNode root) {
        if(root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.key+" ");
        inorder(root.right);
    }
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(12);
        binarySearchTree.insert(21);
        binarySearchTree.insert(5);
        binarySearchTree.insert(18);
        binarySearchTree.insert(9);
        binarySearchTree.inorder(binarySearchTree.root);
        System.out.println();
        BinarySearchTree.TreeNode ret = binarySearchTree.search(12);
        System.out.println(ret.key);
        binarySearchTree.remove(12);
        binarySearchTree.inorder(binarySearchTree.root);
    }
}